package com.burst.reptile.modular.api.controller;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burst.reptile.common.utils.DateUtil;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.framework.constant.ConstantFactory;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.LayuiResult;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.api.form.AnswerForm;
import com.burst.reptile.modular.api.vo.QuestionVo;
import com.burst.reptile.modular.api.warpper.ZpQuestionWrapper;
import com.burst.reptile.modular.system.entity.SysUser;
import com.burst.reptile.modular.zp.entity.ZpQuestion;
import com.burst.reptile.modular.zp.entity.ZpQuestionType;
import com.burst.reptile.modular.zp.service.IZpQuestionService;
import com.burst.reptile.modular.zp.service.IZpQuestionTypeService;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 问问列表 前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-06-29
 */
@RestController
@RequestMapping("/zp/question")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZpQuestionController extends BaseController{
	
	private final IZpQuestionService questionService;
	
	private final IZpQuestionTypeService questionTypeService;
 
	/**
	 * 新增咨询问题
	 * @param question
	 * @return
	 */
	@PostMapping("/add")
	public Result<Object> add(@Validated @RequestBody ZpQuestion question){
//		boolean examineText = TextContent.examineText(question.getQuestion());
//		if(examineText) {
//			return Result.error("发布内容含敏感词汇");
//		}
		return Result.success(questionService.add(question));
	}
	
	/**
	 * 分页获取问题列表
	 * @param keyWord
	 * @param questionType
	 * @return
	 */
	@GetMapping("/qlist")
	public LayuiResult<Object> add(@RequestParam(required = false) String keyWord, @RequestParam(required = false) String questionType){
		Page<ZpQuestion> page = startPage();
		Page<ZpQuestion> pagelist = questionService.pageList(page, keyWord, questionType);
		List<ZpQuestion> records = pagelist.getRecords();
		List<QuestionVo> list = new ArrayList<>();
		if(ToolUtil.isNotEmpty(records)) {
			list = records.stream().map(i -> {
				QuestionVo questionVo = new QuestionVo();
				ZpQuestionType byId = questionTypeService.getById(i.getQuestionType());
				if(ToolUtil.isNotEmpty(byId)) {
					questionVo.setTage(byId.getQuestionType()); // 咨询类别标签
				}
				SysUser user = ConstantFactory.me().getUser(i.getConsultant());
				if(ToolUtil.isNotEmpty(user)) {
					questionVo.setAvatar(user.getAvatar()); // 头像
					questionVo.setName(user.getNickName()); // 咨询师姓名
					questionVo.setGraduateSchool(user.getRemark()); // 毕业学校
				}
				questionVo.setId(i.getId());
				questionVo.setUserName(i.getUserName()); // 咨询用户
				questionVo.setQuestion(i.getQuestion()); // 问题
				questionVo.setAnswer(i.getAnswer()); // 解答
				questionVo.setAnswerTime(i.getAnswerTime()); // 解答时间
				questionVo.setFabulous(i.getFabulous()); // 点赞数量
				int count = questionService.count(new QueryWrapper<ZpQuestion>().eq("consultant", i.getConsultant()));
				questionVo.setZxrs(count); // 咨询人数
				questionVo.setConsultant(i.getConsultant());
				questionVo.setTime(DateUtil.timeToFormat(i.getCreateTime()));
				return questionVo;
			}).collect(Collectors.toList());
		}
		return LayuiResult.success(pagelist.hasNext(), pagelist.getTotal(), list);
	}
	
	/**
	 * 分页获取咨询列表
	 * @return
	 */
	@GetMapping("/list")
	@PreAuthorize("@ss.hasPermi('zp:consulting:list')")
	public LayuiResult<Object> list(ZpQuestion question){
		Page<ZpQuestion> page = startPage();
		Page<ZpQuestion> pageList = questionService.pageList(page, question);
		new ZpQuestionWrapper(pageList).wrap();
		return LayuiResult.success(pageList.getTotal(), pageList.getRecords());
	}
	
	/**
	 * 解答
	 * @return
	 */
	@PostMapping("/answer")
	@PreAuthorize("@ss.hasPermi('zp:consulting:answer')")
	public Result<Object> answer(@Validated @RequestBody AnswerForm answerForm){
		return Result.success(questionService.answer(answerForm));
	}
	
	/**
	 * 用户解答列表
	 * @return
	 */
	@GetMapping("/alist")
	@PreAuthorize("@ss.hasPermi('zp:answer:list')")
	public LayuiResult<Object> alist(ZpQuestion question){
		Page<ZpQuestion> page = startPage();
		Page<ZpQuestion> pageList = questionService.pageAnswerList(page, question);
		return LayuiResult.success(pageList.getTotal(), pageList.getRecords());
	}
	
}
